package com.daniu101;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

/*
* 项目中写道服务层
* */
public class QrWrite {
    public static void main(String[] args) throws WriterException, IOException {
        String url = "http://www.zt221.com/";
        int widthHeight = 500;

        // 阿里云的OSS or 服务器的绝对路径
        String filePathName = "D:\\"+ System.currentTimeMillis() +".png";
        Path path = FileSystems.getDefault().getPath(filePathName);

        /////////////////////////////////////////
        //二维码生成对象QRCodeWriter
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        // 已经是一个图片
        BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, widthHeight, widthHeight);
        // 写到硬盘
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
        /////////////////////////////////////////

        System.out.println("完成");
    }
}
